package com.zhouyibike.uu.ui.activity.mybike;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.zhouyibike.uu.R;
import com.zhouyibike.uu.config.Config;
import com.zhouyibike.uu.entity.MyYiQuanTransferResult;
import com.zhouyibike.uu.http.ApiCallback;
import com.zhouyibike.uu.ui.base.BaseActivity;
import com.zhouyibike.uu.utils.TextUtils;

import java.util.HashMap;
import java.util.Map;

public class ZhuanZengActivity extends BaseActivity {
    private int cansellnum = 0;
    private int sellingnum = 0;
    private Button quedingbt;
    private TextView jiatv, jiantv, canselltv;
    private EditText sellingedit, sellingtv;
    private ImageButton mBtnBack;
    private TextView mBtnTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhuan_zeng);
        cansellnum = getIntent().getIntExtra("cansellnum", 0);
        initView();
        initListener();
    }

    private void initListener() {
        mBtnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        jiatv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sellingnum += 1;
                sellingedit.setText(sellingnum + "");
            }
        });
        jiantv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sellingnum -= 1;
                sellingedit.setText(sellingnum + "");
            }
        });
        sellingedit.addTextChangedListener(new ZhuanZengActivity.EditTextWatcher());
        quedingbt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isNullorEmpty(sellingedit.getText().toString())) {
                    toastShow("请输入转让单车数量");
                }else{
                    if (cansellnum>=sellingnum)
                    {
//                    zhuanrang();
                        toastShow(sellingtv.getText().toString());
                    }else{
                        toastShow("可转让单车数量不足");
                    }
                }

            }
        });
    }

    private void initView() {
        mBtnBack = (ImageButton) findViewById(R.id.bar_btn_back);
        mBtnTitle = (TextView) findViewById(R.id.bar_text_title);
        quedingbt = (Button) findViewById(R.id.yiquanzhuanrang_submit);
        jiatv = (TextView) findViewById(R.id.yiquangongzhuanrangnumtvjia);
        jiantv = (TextView) findViewById(R.id.yiquangongzhuanrangnumtvjian);
        canselltv = (TextView) findViewById(R.id.yiquankezhuanrangnumtv);
        sellingedit = (EditText) findViewById(R.id.zhuanrangedittext);
        sellingtv = (EditText) findViewById(R.id.yiquangongzhuanrangnumtv);
        canselltv.setText(cansellnum + "辆");
        mBtnTitle.setText("转赠单车");
        jiantv.setEnabled(false);
    }

    class EditTextWatcher implements TextWatcher {

        public void afterTextChanged(Editable edt) {
            if (TextUtils.isNullorEmpty(sellingedit.getText().toString())) {
                jiantv.setEnabled(false);
                jiatv.setEnabled(false);
            } else {
                jiatv.setEnabled(true);
                if ( Integer.parseInt(sellingedit.getText().toString())==0) {
                    jiantv.setEnabled(false);
                } else {
                    jiantv.setEnabled(true);
                }
                sellingnum = Integer.parseInt(sellingedit.getText().toString());
            }

        }

        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        }

        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        }

    }
    /**
     * //进行转赠
     */
    private void zhuanrang() {
        Map<String, String> params = new HashMap<>();
        params.put("token", preferences.getString("token",""));
        params.put("count", sellingnum+"");
        showProgressDialog();
        addSubscription(apiStores.MyYiQuanTransfer(params),
                new ApiCallback<MyYiQuanTransferResult>() {

                    @Override
                    public void onSuccess(MyYiQuanTransferResult model) {
                        if(model.getStatus()== Config.SUCCESSCODE){
                            toastShow(model.getMessage());
                            setResult(1);
                        }else{
                            toastShow(model.getMessage());
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        toastShow(msg);

                    }

                    @Override
                    public void onFinish() {
                        dismissProgressDialog();
                    }
                });
    }
}
